﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace AutomatizzazioneCleverConsumit
{
    public partial class Form1 : Form
    {
        private DateTime _lastdate = DateTime.Now;
        bool firsttime = true;

        public Form1()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void configurazioneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Configuration cfgForm = new Configuration();
            cfgForm.Show();
        }

        private bool VerifyConfig()
        {
            if (
                String.IsNullOrEmpty(ConfigUtil.GetEmailSender())
                || String.IsNullOrEmpty(ConfigUtil.GetEmailSenderPassword())
                || String.IsNullOrEmpty(ConfigUtil.GetEmailServer())
                || String.IsNullOrEmpty(ConfigUtil.GetEmailPort())
                || String.IsNullOrEmpty(ConfigUtil.GetEmailDest())
                || String.IsNullOrEmpty(ConfigUtil.GetWebSite())
                || String.IsNullOrEmpty(ConfigUtil.GetWebUser())
                || String.IsNullOrEmpty(ConfigUtil.GetWebPassword())
                || String.IsNullOrEmpty(ConfigUtil.GetHours())
                || String.IsNullOrEmpty(ConfigUtil.GetMinutes())
                || String.IsNullOrEmpty(ConfigUtil.GetInterval())
                || String.IsNullOrEmpty(ConfigUtil.GetOutput())
                || String.IsNullOrEmpty(ConfigUtil.GetLog())
               )
            {
                return false;
            }
            return true;
        }

        private void Scheduler_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.Minute.ToString() == ConfigUtil.GetMinutes()
                && DateTime.Now.Hour.ToString() == ConfigUtil.GetHours())
            {

                if (DateTime.Now.Date == _lastdate.AddDays(int.Parse(ConfigUtil.GetInterval())).Date
                    || firsttime)
                {

                    firsttime = false;
                    _lastdate = DateTime.Now;

                    notifyIcon1.ShowBalloonTip(5000, "Attenzione..", "Processo iniziato", ToolTipIcon.Info);
                    Automation au = new Automation();
                    try
                    {

                        au.ProcessAll(ConfigUtil.GetWebUser(), ConfigUtil.GetWebPassword());

                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex.Message, LogType.Error);
                    }
                    notifyIcon1.ShowBalloonTip(50000, "Attenzione..", "Processo finito", ToolTipIcon.Info);

                }
            }
        }

        private void manualeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (VerifyConfig())
            {
                notifyIcon1.ShowBalloonTip(5000, "Attenzione..", "Processo iniziato", ToolTipIcon.Info);
                Automation au = new Automation();
                try
                {
                    try
                    {
                        Process[] proc = Process.GetProcessesByName("IEXPLORE");
                        foreach (Process procc in proc)
                            procc.Kill();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }

                    au.ProcessAll(ConfigUtil.GetWebUser(), ConfigUtil.GetWebPassword());

                    //au.ProcessAll(UTILS.ConfigUtil.GetWebUser(2), UTILS.ConfigUtil.GetWebPassword(2));
                    //MailUtils.Send("IN ALLEGATO FILE LAVORATO RELATIVO ALLE VOCI DI AGENDA PRESENTI SU EPC, SI PREGA DI VERIFICARNE IL CONTENUTO PRIMA DELL’INVIO IN COMMITTENTE", String.Format("BNL - EVASIONE VOCI DI AGENDA {0}", DateTime.Now.ToString()), UTILS.ConfigUtil.GetExcelPath() + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + "\\" + "evasione" + UTILS.ConfigUtil.GetWebUser(1) + ".xls", UTILS.ConfigUtil.GetExcelPath() + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + "\\" + "evasione" + UTILS.ConfigUtil.GetWebUser(2) + ".xls", UTILS.ConfigUtil.GetEmailDest());
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.Message, LogType.Error);
                }
                notifyIcon1.ShowBalloonTip(5000, "Attenzione..", "Processo finito", ToolTipIcon.Info);
            }
            else
            {
                MessageBox.Show("Si prega di compilare tutti i campi di configurazione!");
            }
        }

        private void pianificataStartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (pianificataStartToolStripMenuItem.Text.ToLower().Contains("start"))
            {
                if (VerifyConfig())
                {
                    try
                    {
                        Process[] proc = Process.GetProcessesByName("IEXPLORE");
                        foreach (Process procc in proc)
                            procc.Kill();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }

                    Scheduler.Enabled = true;
                    Scheduler.Start();
                    pianificataStartToolStripMenuItem.Text = "Pianificata Stop";
                }
                else
                {
                    MessageBox.Show("Si prega di compilare tutti i campi di configurazione!");
                }
            }
            else
            {
                Scheduler.Stop();
                Scheduler.Enabled = false;
                pianificataStartToolStripMenuItem.Text = "Pianificata Start";
            }
        }
    }
}
